<?php
$menu=new MenuMain2($module,$root_permission);

$menu->addP("Menu","module");

BuildMenuModuleE($menu);
BuildMenuModunleAdd($menu);

//$menu->addP("Media");
//$menu->addC(Photos,"gallery");
//$menu->addC(Videos,"video");

//$menu->addP("Review");
//$menu->addC("Review",'review','');
//$menu->addC("Review Hotel",'review','hotel');

$menu->addP(getLang("Site"));
$menu->addC(getLang("Config"),"config");
$menu->addC(getLang("Files"),"files");
$menu->addC(getLang("Banner"),"banner");
$menu->addC(getLang('Account manager'),"account");
$menu->addC(getLang('Permission'),"permission");
$menu->addC(getLang('Tracking'),"tracking");
$menu->addC(getLang('Statistics'),"statistics");
$menu->addC(getLang("Email Templates"),"emailtemp");
$menu->addC(getLang("Advertise"),"advertise");
$menu->addC(getLang("Language"),"lang");
$menu->addC(getLang("Weather"),"weather");

/////////////-------------------------------------///////////////////////////////

function BuildMenuModunleAdd($menu)
{
global $language,$dbsql;

	$sql_select = "select iId,cName,cUrlString,cType from tblcategory
			where  cLanguage='$language' and iParentId=0 and( cType='module' 
                or cType='sitemap' or cType='faqs' or cType='typicalworks')  and cLanguage='$language' order by iPlace,iOrder";

    $rs=$dbsql->query($sql_select);
    if($dbsql->num_rows($rs)>0)
    {
    	$menu->addP("Other","");
    }
	while ( $result = $dbsql->fetch_array($rs) )
	{
		$menu->addC($result["cName"],$result['cUrlString']);
	}
}

function BuildMenuModuleE($menu)
{
global $language,$dbsql;

    $sql_select = "select iId,cName,cUrlString,cType from tblcategory
    	where  cLanguage='$language' and iParentId=0 and cType!='module' and cType!='gallery' and cType!='video'
        and cType!='tourtype' and cType!='sitemap' and cType!='inquiry'  
        and cLanguage='$language' and cType!='faqs' and cType!='typicalworks'
        and iStatus=1 order by iPlace,iOrder";

    $rs=$dbsql->query($sql_select);

    while ( $result = $dbsql->fetch_array($rs) )
    {
        $type   = strtolower($result['cType']);
        $name   = $result['cName'];
        if($type=="promotions")
        {
            $menu->addP($name);
            $menu->addC($name . " Tour",$type,'tour');
            $menu->addC($name . " Hotel",$type,'hotel');
        }
        else if($type== "hotel")
        {
            $menu->addP(getLang('Hotel'));
            $menu->addC($name,"hotel");
            //$menu->addC(getLang("Hotel Type"),"hoteltype");
            //$menu->addC(getLang("Booking"),"bookhotel");
        }
        else if($type=="tour")
        {
            $menu->addP(getLang("Tour"));
            //$menu->addC($name,"tour");
            //$menu->addC(getLang("Tour type"),"tourtype");
            //$menu->addC(getLang("Tour config"),"tour&opt=config");
            //$menu->addC(getLang("Booking"),"booking");
        }
        else if($type=="news")
        {
            $menu->addP($name,$type);
            $menu->addC($name,$type);
           // $menu->addC(getLang("Newsletter"),"newsletter");
        }
        else if($type=="product")
        {
            $menu->addP($name);
            $menu->addC($name,$type);
            $menu->addC(getLang("Category"),"productcat");
            $menu->addC(getLang("Booking"),"booking");
        }
        else
        {
            $menu->addP($name,$type);
        }
    }
}

class  MenuMain2
{
	var $menus;
	var $urls;
	var $childs;
	var $select;
	var $idNow;
    var $header='';
    var $submenu;
    var $module;
    var $listModule=array();
    var $permissions;
    var $toolbar;
	function MenuMain2($module='',$permissions=false)
	{
		$this->header= '';
        $this->menus=array();
		$this->urls=array();
		$this->childs=array();
		$this->idNow=-1;
        $this->permissions=$permissions;
        $this->module=$module;
	}
	function addP($name,$url="")
	{
	   if(!empty($url))
        {
            $this->listModule[$url]=$name;
        }
        if($this->checkPermission($url))
        {
    		$this->idNow++;
    		$this->menus[$this->idNow]=$name;
    		$this->urls[$this->idNow]=$url;
    		$this->childs[$this->idNow]=array();
        }
	}
	function addC($name,$url,$sub='')
	{
	   if(!empty($url))
        {
            if(!empty($sub))
            {
                $url.='&child='.$sub;
            }
            $this->listModule[$url]=$name;
        }
        if($this->checkPermission($url))
        {
    		$arr=array($url=>$name);
    		$this->childs[$this->idNow]=array_merge($this->childs[$this->idNow],$arr);
    		if($this->urls[$this->idNow]=="")
    		{
    			$this->urls[$this->idNow]=$url;
    		}
        }
	}
    function checkPermission($url)
    {
        if(!is_array($this->permissions) || empty($url))return true;
        if($this->permissions[$url]['view']>0)
        {
            return true;
        }
        return false;
    }
    function getModules()
    {
        return $this->listModule;
    }
	function buildMenu()
	{
		$menumain = '<ul id="menu">';
		foreach($this->menus as $id => $name)
		{
			$url=$this->urls[$id];
            $class  = ($url==$this->module)? 'class="selected"': '';
            //load menuchild
            $menuchild='';
			if(count($this->childs[$id] )>0)
			{
				$menuchild.='<ul>';
				foreach($this->childs[$id] as $urlchild=>$namechild)
				{
				    if($urlchild==$this->module || $urlchild==$this->module.'&child='.$_GET['child'])
                    {
                        $this->submenu=$this->childs[$id];
                        $class  = 'class="selected"';
                    }
					$menuchild .= "<li> <a href='?module=".$urlchild."'>".$namechild."</a></li>";
				}
				$menuchild.='</ul>';
			}else if(empty($url))
            {
                continue;
            }

            //parent
			$menumain .= '<li '.$class.'><a href="?module='.$url.'">'.$name.'</a>';
            $menumain .= $menuchild;
			$menumain .= "</li>";
		}
		$menumain .= "</ul>";
        $menumain=$this->header."\n".$menumain;
		return $menumain;
	}
    function buildToolBar($toolbar)
    {
        if(empty($this->module) || $this->module=="index" || !is_object($toolbar))return;
        //print_r($this->listModule);
        $this->toolbar  = $toolbar;
        if($toolbar->getTitleModel()==true)
        {
            $title  = $toolbar->getTitle();
        }else
        {
            $title  = $this->listModule[$this->module].' '.$toolbar->getTitle();
        }
        if(empty($this->listModule[$this->module]))
        {
            $title = $this->listModule[$this->module.'&child='.$_GET['child']].' '.$toolbar->getTitle();
        }
        $html='<div class="toolbar"><div class="bor">';
        $html.='<div class="icon"></div>';
        $html.='<div class="t">'.$title.'</div>';
        $html.='<div class="buts">'.$toolbar->getHTML().'</div>';
        $html.='</div></div>';
        return $html;
    }
    function buildSubMenu()
    {
        global $act;
        $html='';
        if($this->toolbar!=null && $this->toolbar->getTabs())
        {
            $html.='<div class="submenu"><div id="Tabs">';
            foreach($this->toolbar->getTabs() as $i=>$name)
            {
                $html.='<a href="#'.$name.'">'.$name.'</a>';
            }
            $html.='</div></div>';
        }
        else if(is_array($this->submenu))
        {
            $html.='<div class="submenu"><div>';
            foreach($this->submenu as $url=>$name)
            {
                $html.='<a ';
                if($this->module==$url || $url==$this->module.'&child='.$_GET['child'])
                {
                    $html.='class="selected"';
                }
                if($this->module.'&opt='.$act == $url && strpos($url,"opt")!==false)
                {
                    $html.='class="selected"';
                }
                $html.=' href="?module='.$url.'">'.$name.'</a>';
            }
            $html.='</div></div>';
        }
        return $html;
    }
    function initTabs()
    {
        if($this->toolbar!=null && $this->toolbar->getTabs())
        {
            return '<script>tabview_initialize("Tabs");</script>';
        }
    }
}
?>